<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>

<body>
    <div id="app" style="border: 1px solid green; padding:15px">
        <div>这里是父组件的区域 --> {{parentMsg}}</div>
        <children @on-push="getPush"></children>
        <uncle v-if="parentMsg" :m="parentMsg"></uncle>
    </div>
    <script>
        new Vue({
            el: "#app",
            data: {
                parentMsg: ""
            },
            components: {
                uncle: {
                    props: ['m'],
                    template: `
                        <div style='border:1px solid orange;height:50px'>{{m}}</div>
                    `
                },
                children: {
                    template: `
                    <div style="border:1px solid red">{{msg}}
                        <button @click="pushParent">向父组件给东西</button>
                        <button @click="pushSon" v-if="getson">向父组件给东西</button>
                        <son @on-change="getSon"></son>
                        </div>
                    `,
                    data() {
                        return {
                            msg: "子组件的内容",
                            getson: ""
                        }
                    },
                    methods: {
                        pushParent() {
                            this.$emit('on-push', this.msg)
                        },
                        getSon(val) {
                            this.getson = val
                            //this.$emit('on-push',val)
                        },
                        pushSon() {
                            this.$emit('on-push', this.getson)
                        }
                    },
                    components: {
                        son: {
                            template: `
                            <div>111
                                <button @click="push">孙子要给爷爷送点东西</button>
                            </div>
                        `,
                            methods: {
                                push() {
                                    this.$emit('on-change', '今天天气真好')
                                }
                            }
                        }
                    }
                }
            },
            methods: {
                getPush(val) {
                    this.parentMsg = val
                }
            }
        })
    </script>
</body>

</html>